package com.xuerhua.yxt.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.model.ClassTeachers;
import com.xuerhua.yxt.model.StudentInfo;
import com.xuerhua.yxt.reqVo.BaseQueryVO;
import com.xuerhua.yxt.reqVo.ParentReqVO;
import com.xuerhua.yxt.service.IClassTeachersService;
import com.xuerhua.yxt.service.IOrganizeGroupService;
import com.xuerhua.yxt.service.IParentService;
import com.xuerhua.yxt.service.IStudentService;
import com.xuerhua.yxt.service.ITeacherService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.view.ParentRelStudentView;
import com.xuerhua.yxt.view.StudentInfoView;
import com.xuerhua.yxt.view.TeacherInfoView;

/**
 * 用户角色控制器
 *
 * @author huqimeng
 * @date 2017年5月21日
 */
@Controller
public class UserRoleController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(UserRoleController.class);

    @Autowired
    private IStudentService studentService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IClassTeachersService classTeachersService;
    @Autowired
    private IOrganizeGroupService organizeGroupService;
    @Autowired
    private IParentService parentService;

    /**
     * 家长首页
     *
     * @param model
     * @return
     */
    @RequestMapping(Const.STUDENT_ROLE)
    public String studentIndex(Model model) {
        try {
            String sid = request.getParameter("sid");
            model.addAttribute("sid", sid);
            return "student/index";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 老师首页
     *
     * @param model
     * @return
     */
    @RequestMapping(Const.TEACHER_ROLE)
    public String teacherIndex(HttpServletResponse response, Model model) {
        try {
            return "teacher/index";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 家长对应学生列表
     *
     * @param model
     * @return
     */
    @RequestMapping(Const.STUDENT_ROLE + MenuConst.USER_ROLE_URL)
    public String studentRole(Model model) {
        List<StudentInfo> list = studentService.selectByOpenid(getOpenid());
        List<StudentInfoView> listView = BeanUtils.copyAs(list, StudentInfoView.class);
        model.addAttribute("list", listView);
        return "role/studentRole";
    }

    /**
     * 老师对应班级列表
     *
     * @param model
     * @return
     */
    @RequestMapping(Const.TEACHER_ROLE + MenuConst.USER_ROLE_URL)
    public String teacherRole(Model model) {
        AppUser appUser = getAppUser();
        if (organizeGroupService.isLeaderGroup(appUser.getTeacher().getId()) > 0) {
            BaseQueryVO vo = new BaseQueryVO();
            vo.setFinishYear(getCurrentTerm().getFinishYear());
            vo.setSchoolId(appUser.getTeacher().getSchoolId());
            List<ClassTeachers> list = classTeachersService.selectClassList(vo);
            model.addAttribute("leaderList", getGradeClassList(list));
        } else {
            List<TeacherInfoView> list = teacherService.selectClazzListByOpenid(appUser.getOpenid());
            model.addAttribute("list", list);
        }
        model.addAttribute("redirectUrl", request.getParameter("redirectUrl"));
        return "role/clazzRole";
    }

    @RequestMapping(Const.STUDENT_ROLE + "/graduate")
    public String graduate() {
        return "graduate";
    }

    @RequestMapping(value = {Const.STUDENT_ROLE + "/student/unbind", Const.TEACHER_ROLE + "/student/unbind"})
    public String studentUnbind(Model model) {
        AppUser appUser = getAppUser();
        Integer schoolId = appUser.getRoleType() == 1 ? appUser.getTeacher().getSchoolId() : appUser.getParent().getSchoolId();
        List<ParentRelStudentView> list = parentService.selectRelStudentByOpenid(appUser.getOpenid(), schoolId);
        model.addAttribute("list", list);
        return "teacher/studentUnbind";
    }

    @RequestMapping("student/unbind/submit")
    @ResponseBody
    Object studentUnbindSubmit(ParentReqVO vo) {
        try {
            AppUser appUser = getAppUser();
            Integer schoolId = appUser.getRoleType() == 1 ? appUser.getTeacher().getSchoolId() : appUser.getParent().getSchoolId();
            vo.setOpenid(appUser.getOpenid());
            vo.setSchoolId(schoolId);
            parentService.delByRelId(vo);
            return new RspResult(Status.SUCCESS);
        } catch (Exception e) {
            logger.error("--studentUnbindSubmit error={}", e);
        }
        return new RspResult(Status.FAIL);
    }
}
